using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using CloningExtension;
using UnityEngine;

public class PlayerPrefsSerializer
{
	public static bool handleNonSerializable
	{
		set
		{
			PlayerPrefsSerializer.bf.SurrogateSelector = new SurrogateSelector();
			PlayerPrefsSerializer.bf.SurrogateSelector.ChainSelector(new NonSerialiazableTypeSurrogateSelector());
		}
	}

	public static void Save(string prefKey, object serializableObject)
	{
		MemoryStream memoryStream = new MemoryStream();
		PlayerPrefsSerializer.bf.Serialize(memoryStream, serializableObject);
		string value = Convert.ToBase64String(memoryStream.ToArray());
		PlayerPrefs.SetString(prefKey, value);
	}

	public static T Load<T>(string prefKey)
	{
		if (!PlayerPrefs.HasKey(prefKey))
		{
			return default(T);
		}
		string @string = PlayerPrefs.GetString(prefKey);
		MemoryStream serializationStream = new MemoryStream(Convert.FromBase64String(@string));
		return (T)((object)PlayerPrefsSerializer.bf.Deserialize(serializationStream));
	}

	public static string Encode(object serializableObject)
	{
		MemoryStream memoryStream = new MemoryStream();
		PlayerPrefsSerializer.bf.Serialize(memoryStream, serializableObject);
		return Convert.ToBase64String(memoryStream.ToArray());
	}

	public static T Decode<T>(string serialized)
	{
		MemoryStream serializationStream = new MemoryStream(Convert.FromBase64String(serialized));
		return (T)((object)PlayerPrefsSerializer.bf.Deserialize(serializationStream));
	}

	public static BinaryFormatter bf = new BinaryFormatter();
}
